/*
提交链接:https://leetcode.cn/problems/subarray-sum-equals-k/description/
560. 和为 K 的子数组
赖德檀 2024/12/13
*/

class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        unordered_map<int,int>mp;
        mp[0]=1;
        int ans=0;
        int sum=0;
        for(int i=0;i<nums.size();i++)
        {
            sum+=nums[i];
            if(mp.find(sum-k)!=mp.end())
            ans+=mp[sum-k];
            mp[sum]+=1;
        }
        return ans;
    }
};